package com.yc.backstage.service.serviceImpl;

import com.yc.backstage.entity.SysUser;
import com.yc.backstage.entity.SysUserPost;
import com.yc.backstage.mapper.SysUserPostMapper;
import com.yc.backstage.service.SysUserPostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname SysUserPostController
 * @Description
 * @Version 1.0.0
 * @Date 2022-08-23 11:49:19
 * @Created by Mikechen
 */
@Service
public class SysUserPostServiceImpl extends ServiceImpl<SysUserPostMapper, SysUserPost> implements SysUserPostService {

    @Autowired
    private SysUserPostMapper sysUserPostMapper;

    /**
     * 新增用户岗位关联
     *
     * @param user
     */
    @Override
    public void insertUserPost(SysUser user) {
        Long[] posts = user.getPostIds();
        if (StringUtils.isNotEmpty(posts)) {
            // 新增用户与岗位管理
            List<SysUserPost> list = new ArrayList<SysUserPost>(posts.length);
            for (Long postId : posts) {
                SysUserPost up = new SysUserPost();
                up.setUserId(user.getUserId());
                up.setPostId(postId);
                list.add(up);
            }
            sysUserPostMapper.batchUserPost(list);
        }
    }

    /**
     * 通过用户ID删除用户和岗位关联
     *
     * @param userId 用户ID
     * @return 结果
     */
    public int deleteUserPostByUserId(Long userId) {
        return sysUserPostMapper.deleteUserPostByUserId(userId);
    }

    /**
     * 批量删除用户和岗位关联
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteUserPost(List<Long> ids) {
        return sysUserPostMapper.deleteUserPost(ids);
    }
}
